﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dataweb.NShape;
using System.Windows.Forms;
using cz.zcu.PreSti.Shapes;

namespace cz.zcu.PreSti.Guide
{
	/// <summary>
	/// Represents sequential type of guide.
	/// </summary>
	public class RandomGuide : GuideParent
	{	  
		//Variables
		private int totalStimuliNumber = 5;
		private int totalDuration = 5000;
		private int shapePossitionY = 300;
		private int splitterPossitionY = 400;
		Boolean firstPage = true;        
		private Boolean exception;
		private const int maxStimuli = 4;
		private int countStimuli = 0;
		
		//Lists
		private List<TemplatesShape> listSplitter;
		private List<TemplatesShape> listDiscreteGenerators;
		private List<TemplatesShape> listConnectors;

		/// <summary>
		/// Initializes a new instance of the <see cref="RandomGuide"/> class.
		/// </summary>
		/// <param name="project">The project.</param>
		/// <param name="diagram">The diagram.</param>
		/// <param name="listProperties">The list properties.</param>
		/// <param name="window">The window.</param>
		public RandomGuide(Project project, Diagram diagram, List<Property> listProperties, Form window)
		{
			InitializeParametres(project, diagram, listProperties, window);

			listSplitter = new List<TemplatesShape>();
			listDiscreteGenerators = new List<TemplatesShape>();
			listConnectors = new List<TemplatesShape>();
			EventsRegistration();
			LoadPage1();
		}

		/// <summary>
		/// Registrates the events to the controls.
		/// </summary>
		private void EventsRegistration()
		{
			listbox.Click += listbox1_Click; 
			checkBox.CheckedChanged += CBHelp_CheckedChanged;
			cbAlignmentH.Leave += TextBoxs_Leave;
			cbAlignmentV.Leave += TextBoxs_Leave;
			cbAlignmentH.SelectedIndexChanged += CB_SelectedIndexChanged;
			cbAlignmentV.SelectedIndexChanged += CB_SelectedIndexChanged;

			TextsBoxesEventRegistration();
			btNext.Click += BTNext_Click;
			btPrevious.Click += BTPrevious_Click;
			btDelete.Click += BT_Delete;
			btAdd.Click += BTAdd_Click;
			gbProperties.MouseLeave += GB_Mouse_Leave; 
			btPath.Click += BTPath_Click;
		}

		/// <summary>
		/// Handles the Click event of the BTPath control: provides a possibility to enter path of image from operation system.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void BTPath_Click(object sender, EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog();

			if (ofd.ShowDialog() == DialogResult.OK)
			{
				listProperties[2].TextBox.Text = ofd.FileName;
			}
		}

		/// <summary>
		/// Handles the Leave event of the GB_Mouse control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void GB_Mouse_Leave(object sender, EventArgs e)
		{
			if (listbox.SelectedItem != null)
			{
				if (exception == false)
				{
					TextBoxs_Leave(new object(), new EventArgs());
				}
			}
		}

		/// <summary>
		/// Handles the Click event of the BTAdd control: adds stimuli to the shape list.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void BTAdd_Click(object sender, EventArgs e)
		{
			if (countStimuli == maxStimuli)
			{
				MessageBox.Show("Guide supports only 4 stimuli");
			}
			else
			{
				AddStimuli(cbStimuliType.SelectedItem.ToString());
				countStimuli++;
			}
		}

		/// <summary>
		/// The Event registration of text boxes.
		/// </summary>
		private void TextsBoxesEventRegistration()
		{
			for (int i = 0; i < listProperties.Count; i++)
			{
				listProperties[i].TextBox.Leave += TextBoxs_Leave;
			}
		}
		/// <summary>
		/// The initialize of the first page.
		/// </summary>
		private void LoadPage1()
		{

			if (checkBox.Checked == true)
			{

				window.ClientSize = new System.Drawing.Size(1150, 600);
			}
			else
			{
				window.ClientSize = new System.Drawing.Size(470, 600);
			}

			UnvissibleItems(1);
			VissibleItems(1);

			listProperties[0].Label.Text = "Total numer of stimuli";
			listProperties[1].Label.Text = "Total duration [ms]";

			listProperties[0].TextBox.Text = totalStimuliNumber.ToString();
			listProperties[1].TextBox.Text = totalDuration.ToString();

			btPath.Visible = false;
			btCreateGraph.Visible = false;
			btRun.Visible = false;
			cbAlignmentH.Visible = false;
			cbAlignmentV.Visible = false;
			lbNoStimulus.Visible = false; 
			btPrevious.Visible = false;            
			gbStimuli.Visible = false;
			gbProperties.Visible = true;
			btNext.Visible = true;

			lbTitle.Text = "Random experiment guide";
			gbProperties.Text = "Properties";
			lbSubtitle.Text = "General experiment settings";
			lbText.Text = "This page allows you to set properties that have influence on the whole group of stimuli.";
			lbHelpText1.Text = "1. The random experiment guide works with a group of stimuli. This page provides properties that are defined to work with this group. In the next page you can work only with a particular stimulus.\n\n2. Total number of stimuli defines all stimuli that are inside the group." +
			"\n\n3. Total duration is a time including presentation of one stimulus and interstimulus interval (a time between end of stimulus and beginning of another stimulus). Total duration = stimulus interval + interstimulus interval. For example, if total duration = 5000 [ms], stimulus interval = 3000 [ms] => interstimulus interval = 2000 [ms]. \n\n";

			lbHelpText2.Text = "1.	 Set a total number of stimuli\n\n2. Set total duration\n\n3. Click on the button Next\n\n";
			firstPage = true;
		}
		/// <summary>
		/// The initialize of the second page.
		/// </summary>
		private void LoadPage2()
		{
			UnvissibleItems(1);
			VissibleItems(0);
			listProperties[0].Label.Visible = false;
			listProperties[0].TextBox.Visible = false;

			if (checkBox.Checked == true)
			{

				window.ClientSize = new System.Drawing.Size(1150, 600);
			}
			else
			{
				window.ClientSize = new System.Drawing.Size(680, 600);
			}

			if (listbox.SelectedItem != null)
			{
				AppearModel(listShapes[listbox.SelectedIndex]);
				lbNoStimulus.Visible = false;
			}
			else
			{
				cbAlignmentH.Visible = false;
				cbAlignmentV.Visible = false;
				lbNoStimulus.Visible = true;
				lbNoStimulus.Text = "No stimulus choosen";               
			}

			btNext.Visible = false;
			btCreateGraph.Visible = true;
			btRun.Visible = true;
			btPrevious.Visible = true;
			gbProperties.Visible = true;
			gbStimuli.Visible = true;

			gbProperties.Text = "Properties";
			lbSubtitle.Text = "Stimuli creating with particular settings";
			lbText.Text = "This page makes possible to create stimuli and set its particular properties.";
			lbHelpText1.Text = "This page provides tools that allow creating stimuli, set its properties like probability, duration and others. When you want to create stimulus, you have to choose one of three types of stimuli - Text, Image or Sound type of stimulus. After creating at least one stimulus you can set or change its properties. You can run your experiment.";
			lbHelpText2.Text = "1. Choose one of the stimulus type in the drop-down list placed in the Add stimuli panel on the right side of the window.\n\n2. Validate your choice by pressing Add button.\n\n3. Set properties of already added stimuli by selecting stimuli in the list that is placed under the add button.\n\n4. Properties of particular stimuli appear in the Properties panel in the down – right position. If you want to change some stimulus property you have to click on the property textbox and enter its value.\n\n5. You start to run the experiment and create its graph by clicking on the button Run or create its graph by clicking on the button Create.\n ";
			firstPage = false;
		}

		/// <summary>
		/// Handles the Delete event of the BT control: deletes stimulus on the actual listbox position from the list of shapes and from listbox.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void BT_Delete(object sender, EventArgs e)
		{
			if (listbox.SelectedItem != null)
			{                
				List<TemplatesShape> actualizedList = new List<TemplatesShape>();

				int index = 0;
				foreach (TemplatesShape item in listShapes)
				{
					if (index != listbox.SelectedIndex)
					{
						actualizedList.Add(item);
					}             
					index++;
				}
				listShapes = actualizedList;
				listbox.Items.RemoveAt(listbox.SelectedIndex);

				if (listbox.SelectedItem != null)
				{
					AppearModel(listShapes[listbox.SelectedIndex]);
				}
				else
				{
					LoadPage2(); 
					if (listShapes.Count == 0)
					{
						btCreateGraph.Enabled = false;
						btRun.Enabled = false;
					}
				}
				countStimuli--;
			}
		}

		/// <summary>
		/// Handles the Leave event of the TextBoxs control: saves a textboxes content.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void TextBoxs_Leave(object sender, EventArgs e)
		{
			if (firstPage == true)
			{
				try
				{
					if (listProperties[0].TextBox.Text != null) totalStimuliNumber = Int32.Parse(listProperties[0].TextBox.Text);
					if (listProperties[1].TextBox.Text != null) totalDuration = Int32.Parse(listProperties[1].TextBox.Text);
				}
				catch (Exception)
				{
					MessageBox.Show("Enter only number");
				}
			}
			else
			{
				SaveModel(listShapes[listbox.SelectedIndex]);
			}
		}

		/// <summary>
		/// Handles the SelectedIndexChanged event of the CB control: provides an interaction between comboboxes and position textboxes.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void CB_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (cbAlignmentH.Text == "none") listProperties[5].TextBox.Enabled = true;
			else listProperties[5].TextBox.Enabled = false;

			if (cbAlignmentV.Text == "none") listProperties[6].TextBox.Enabled = true;
			else listProperties[6].TextBox.Enabled = false;
		}

		/// <summary>
		/// Handles the Click event of the BTNext control.
		/// </summary>
		/// <param name="sender">The source of the event: appears the second page.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void BTNext_Click(object sender, EventArgs e)
		{
			LoadPage2();
		}

		/// <summary>
		/// Handles the Click event of the BTPrevious control.
		/// </summary>
		/// <param name="sende">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void BTPrevious_Click(object sende, EventArgs e)
		{
			LoadPage1();
		}

		/// <summary>
		/// Handles the Click event of the listbox1 control: appears model data of particular stimulus.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void listbox1_Click(object sender, EventArgs e)
		{
			if (listbox.SelectedItem != null)
			{
				lbNoStimulus.Visible = false;
				int index = listbox.SelectedIndex;
				gbProperties.Text = listbox.SelectedItem.ToString();
				AppearModel(listShapes[listbox.SelectedIndex]);
			}
		}

		/// <summary>
		/// Handles the CheckedChanged event of the CBHelp control: provides appearing of help window.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void CBHelp_CheckedChanged(object sender, EventArgs e)
		{
			if (firstPage == true)
			{
				if (checkBox.Checked == true) window.ClientSize = new System.Drawing.Size(1150, 600);
				else window.ClientSize = new System.Drawing.Size(470, 600);
			}
			else
			{
				if (checkBox.Checked == true) window.ClientSize = new System.Drawing.Size(1150, 600);
				else window.ClientSize = new System.Drawing.Size(680, 600);
			}
		}

		/// <summary>
		/// Connects two shapes with connector, that is placed in the connector list.
		/// </summary>
		/// <param name="shape1">The shape1.</param>
		/// <param name="shape2">The shape2.</param>
		/// <param name="point1">The point1: glue point of shape.</param>
		/// <param name="point2">The point2: glue point of shape.</param>
		private void ConnectConnectorsShappes(Shape shape1, Shape shape2, int point1, int point2)
		{
			listConnectors[listConnectors.Count - 1].Shape.Connect(1, shape1, point1);
			listConnectors[listConnectors.Count - 1].Shape.Connect(2, shape2, point2);
		}

		/// <summary>
		/// Adds the static part of a graph. It's fixed part. An user can't change that.
		/// </summary>
		protected override void AddStaticGraphPart()
		{
			//Entry
			Template tempEntry = project.Repository.GetTemplate("EntryPoint");
			Shape shapeEntry = tempEntry.CreateShape();
			shapeEntry.X = 400;
			shapeEntry.Y = 400;

			//Repeater
			Template tempRepeater = project.Repository.GetTemplate("Repeater");
			Shape shapeRepeater = tempRepeater.CreateShape();
			RepeaterModel modelRepeater = (RepeaterModel)shapeRepeater.ModelObject;
			modelRepeater.RepetitionRate = totalStimuliNumber;
			modelRepeater.RepetitionPeriod = totalDuration;
			shapeRepeater.X = 450;
			shapeRepeater.Y = 400;

			//DiscreteGenerator			
			Template tempGenerator = project.Repository.GetTemplate("DiscreteGenerator");
			Shape shapeGenerator = tempGenerator.CreateShape();
			DiscreteGeneratorModel modelGenerator = (DiscreteGeneratorModel)shapeGenerator.ModelObject;
			shapeGenerator.X = 550;
			shapeGenerator.Y = 300;
			TemplatesShape shapeDiscreteGenerator = new TemplatesShape(shapeGenerator, modelGenerator, "DiscreteGenerator");

			//Triggers
			Template tempTrigger = project.Repository.GetTemplate("Trigger");
			Shape shapeTrigger = tempTrigger.CreateShape();
			TriggerModel model = (TriggerModel)shapeTrigger.ModelObject;

			Template tempTrigger2 = project.Repository.GetTemplate("Trigger");
			Shape shapeTrigger2 = tempTrigger2.CreateShape();
			TriggerModel modelTrigger = (TriggerModel)shapeTrigger2.ModelObject;

			Template tempTrigger3 = project.Repository.GetTemplate("Trigger");
			Shape shapeTrigger3 = tempTrigger3.CreateShape();
			TriggerModel modelTrigger3 = (TriggerModel)shapeTrigger3.ModelObject;

			Template tempTrigger4 = project.Repository.GetTemplate("Trigger");
			Shape shapeTrigger4 = tempTrigger4.CreateShape();
			TriggerModel modelTrigger4 = (TriggerModel)shapeTrigger4.ModelObject;

			shapeTrigger.Connect(1, shapeEntry, 1);
			shapeTrigger.Connect(2, shapeRepeater, 1);

			ConnectDynamicShapes(shapeRepeater, shapeDiscreteGenerator);
			InsertProbabilityItems(shapeDiscreteGenerator);
			SetSplittersIndexes();

			project.Repository.Insert(shapeEntry, diagram);
			project.Repository.Insert(shapeRepeater.ModelObject);
			project.Repository.Insert(shapeRepeater, diagram);
			project.Repository.Insert(shapeGenerator.ModelObject);
			project.Repository.Insert(shapeGenerator, diagram);
			project.Repository.Insert(shapeTrigger.ModelObject);
			project.Repository.Insert(shapeTrigger, diagram);
			project.Repository.Insert(shapeTrigger2.ModelObject);
			project.Repository.Insert(shapeTrigger2, diagram);
			project.Repository.Insert(shapeTrigger3.ModelObject);
			project.Repository.Insert(shapeTrigger3, diagram);
			project.Repository.Insert(shapeTrigger4.ModelObject);
			project.Repository.Insert(shapeTrigger4, diagram);
			CountGeneratorItemsIndexes();
		}

		/// <summary>
		/// Adds the dynamic part of a graph It's optional part. An user can change this graph part during working with the application.
		/// </summary>
		protected override void AddDynamicGraphPart()
		{
			foreach (TemplatesShape item in listShapes)
			{
				item.Shape.Y = shapePossitionY;
				item.Shape.X = 700;
				shapePossitionY += 100;
			}
			foreach (TemplatesShape item in listShapes)
			{
				project.Repository.Insert(item.Model);
				project.Repository.Insert(item.Shape, diagram);
			}

			foreach (TemplatesShape item in listTriggers)
			{
				project.Repository.Insert(item.Model);
				project.Repository.Insert(item.Shape, diagram);
			}

			foreach (TemplatesShape item in listSplitter)
			{
				item.Shape.Y = splitterPossitionY;
				item.Shape.X = 550;
				project.Repository.Insert(item.Model);
				project.Repository.Insert(item.Shape, diagram);
				splitterPossitionY += 100;
			}
			foreach (TemplatesShape item in listConnectors)
			{
				project.Repository.Insert(item.Shape, diagram);
			}

			foreach (TemplatesShape item in listDiscreteGenerators)
			{
				project.Repository.Insert(item.Model);
				project.Repository.Insert(item.Shape, diagram);
			}

			project.Repository.Update();
		}

		/// <summary>
		/// Counts the splitters indexes.
		/// </summary>
		/// <returns>indexArray: array of splitter indexes</returns>
		private int[] CountSplittersIndexes()
		{
			int itemsCount = 4 * listSplitter.Count;
			int splitterValue = 1;
			int counter = 0;

			int[] indexArray = new int[itemsCount];
			for (int i = 0; i < itemsCount; i++)
			{
				if (counter == 4) //4turns
				{
					counter = 0;
					splitterValue++;

					indexArray[i] = splitterValue;
					splitterValue++;
					counter++;
				}
				else
				{
					indexArray[i] = splitterValue;
					splitterValue++;
					counter++;
				}
			}
			return indexArray;
		}

		/// <summary>
		/// Sets the splitters indexes.
		/// </summary>
		private void SetSplittersIndexes()
		{
			SplitterModel model = null ;
			
			int[] array = CountSplittersIndexes();
			String[] arrayValues;
			int counter = 0;
			int splitterCounter = 0;
			arrayValues = new String[4];

			for (int i = 0; i < array.Length; i++)
			{
				if (counter == 0)
				{
					arrayValues = new String[4];
					model = (SplitterModel)listSplitter[splitterCounter].Shape.ModelObject;
				}
				if (counter == 4) 
				{
					model = (SplitterModel)listSplitter[splitterCounter].Shape.ModelObject;
					counter = 0;
					arrayValues = new String[4];
					arrayValues[counter] = (i + 1).ToString();
					counter++;
				}
				else
				{
					arrayValues[counter] = (i + 1).ToString();
					counter++;
					if (counter == 4)
					{
						model.Values = arrayValues;
						splitterCounter++;
					}
				}			 
			}			 
		}

		/// <summary>
		/// Inserts the probability items: sets generator probability from shape list.
		/// </summary>
		/// <param name="discreteGenerator">The discrete generator.</param>
		private void InsertProbabilityItems(TemplatesShape discreteGenerator)
		{
			DiscreteGeneratorModel model = (DiscreteGeneratorModel) discreteGenerator.Model;
			int[] array = CountGeneratorItemsIndexes();
			for (int i = 0; i < listShapes.Count; i++)
			{
				DiscreteGeneratorModel.DiscreteGeneratorPossibility possibilityItem = new DiscreteGeneratorModel.DiscreteGeneratorPossibility();
				possibilityItem.Probability = listShapes[i].Probability;
				possibilityItem.Value = (i + 1).ToString();
				model.Values.Add(possibilityItem);
			}
		}

		/// <summary>
		/// Counts the generator items indexes.
		/// </summary>
		/// <returns>array of generator indexes</returns>
		private int[] CountGeneratorItemsIndexes()
		{
			int splitterValue = 1;
			int counter = 0;

			int[] indexArray = new int[listShapes.Count];
			for (int i = 0; i < listShapes.Count; i++)
			{
				if (counter == 4) //4turns
				{
					counter = 0;
					splitterValue++;

					indexArray[i] = splitterValue;
					splitterValue++;
					counter++;
				}
				else
				{
					indexArray[i] = splitterValue;
					splitterValue++;
					counter++;
				}
			}
			return indexArray;
		}

		/// <summary>
		/// Connects the dynamic shapes.
		/// </summary>
		/// <param name="repeater">The repeater.</param>
		/// <param name="tempShape">The temp shape.</param> 
		private void ConnectDynamicShapes(Shape repeater, TemplatesShape tempShape)
		{
			int splitterCount = 0;
			int splitterIndexPoint = 2;
			for (int i = 0; i < listShapes.Count; i++)
			{
				if (i % 4 == 0)
				{
					splitterIndexPoint = 2;

					listSplitter.Add(symbolsCreater.AddSplitter());
					listTriggers.Add(symbolsCreater.AddTrigger());
					ConnectTriggShapes(repeater, listSplitter[listSplitter.Count - 1].Shape, 2, 1);
					splitterCount++;

					listConnectors.Add(symbolsCreater.AddConnector());
					ConnectConnectorsShappes(listSplitter[listSplitter.Count - 1].Shape, tempShape.Shape, 7, 1);

					for (int j = i; j < i + 4; j++)
					{
						if (j < listShapes.Count)
						{
							listTriggers.Add(symbolsCreater.AddTrigger());
							ConnectTriggShapes(listSplitter[listSplitter.Count - 1].Shape, listShapes[j].Shape, splitterIndexPoint, 1);
							splitterIndexPoint++;                            
						}
					}
				}
			}
		}

		/// <summary>
		/// Saves edited properties in textboxes to the model.
		/// </summary>
		/// <param name="templatesShape">The templates shape.</param>
		private void SaveModel(TemplatesShape templatesShape)
		{
			if (templatesShape.Type == "Text")
			{
				SaveTextModel(templatesShape);
			}
			if (templatesShape.Type == "Image")
			{
				SaveImageModel(templatesShape);
			}
			if (templatesShape.Type == "Sound")
			{
				SaveSoundModel(templatesShape);
			}			
		}

		/// <summary>
		/// Saves the text model.
		/// </summary>
		/// <param name="templatesShape">The templates shape.</param>
		private void SaveTextModel(TemplatesShape templatesShape)
		{     
			TextStimulusModel model = (TextStimulusModel)templatesShape.Shape.ModelObject;
			exception = false;
			try
			{
				if (listProperties[0].TextBox.Text != null) templatesShape.Probability = Int32.Parse(listProperties[0].TextBox.Text);
				if (listProperties[1].TextBox.Text != null) model.Duration = Int32.Parse(listProperties[1].TextBox.Text);
				if (listProperties[5].TextBox.Text != null) model.X = Int32.Parse(listProperties[5].TextBox.Text);
				if (listProperties[6].TextBox.Text != null) model.Y = Int32.Parse(listProperties[6].TextBox.Text);
				if (listProperties[7].TextBox.Text != null) model.FontSize = Int32.Parse(listProperties[7].TextBox.Text);
			}
			catch (Exception)
			{
				exception = true;
				MessageBox.Show("Enter only number");
			}
			model.Text = listProperties[2].TextBox.Text;
			model.AlignmentHorizontal = cbAlignmentH.Text;
			model.AlignmentVertical = cbAlignmentV.Text;			
		}
		/// <summary>
		/// Saves the image model.
		/// </summary>
		/// <param name="templatesShape">The templates shape.</param>
		private void SaveImageModel(TemplatesShape templatesShape)
		{
			ImageStimulusModel model = (ImageStimulusModel)templatesShape.Shape.ModelObject;
			exception = false;
			try
			{
				if (listProperties[0].TextBox.Text != null) templatesShape.Probability = Int32.Parse(listProperties[0].TextBox.Text);
				if (listProperties[1].TextBox.Text != null) model.Duration = Int32.Parse(listProperties[1].TextBox.Text);
				if (listProperties[3].TextBox.Text != null) model.X = Int32.Parse(listProperties[3].TextBox.Text);
				if (listProperties[4].TextBox.Text != null) model.Y = Int32.Parse(listProperties[4].TextBox.Text);
			}
			catch (Exception)
			{
				exception = true;
				MessageBox.Show("Enter only number");
			}

			model.ImagePath = listProperties[2].TextBox.Text;
		}

		/// <summary>
		/// Saves the sound model.
		/// </summary>
		/// <param name="templatesShape">The templates shape.</param>
		private void SaveSoundModel(TemplatesShape templatesShape)
		{
			SoundStimulusModel model = (SoundStimulusModel)templatesShape.Shape.ModelObject;
			exception = false;
			try
			{
				if (listProperties[0].TextBox.Text != null) templatesShape.Probability = Int32.Parse(listProperties[0].TextBox.Text);
				if (listProperties[1].TextBox.Text != null) model.Duration = Int32.Parse(listProperties[1].TextBox.Text);
			}
			catch (Exception)
			{

				exception = true;
				MessageBox.Show("Enter only number");
			}
		}

		/// <summary>
		/// Appears the model that was selected in the listbox.
		/// </summary>
		/// <param name="templatesShape">The templates shape.</param>
		private void AppearModel(TemplatesShape templatesShape)
		{
			if (templatesShape.Type == "Text")
			{
				AppearTextModel(templatesShape);
			}
			if (templatesShape.Type == "Image")
			{
				AppearImageModel(templatesShape);
			}
			if (templatesShape.Type == "Sound")
			{
				AppearSoundModel(templatesShape);
			}
		}

		/// <summary>
		/// Appears the text model.
		/// </summary>
		/// <param name="templatesShape">The templates shape.</param>
		private void AppearTextModel(TemplatesShape templatesShape)
		{
			TextStimulusModel model = (TextStimulusModel)templatesShape.Shape.ModelObject;

			listProperties[0].Label.Text = "Probability [%]";
			listProperties[0].TextBox.Text = templatesShape.Probability.ToString();

			listProperties[1].Label.Text = "Duration [ms]";
			listProperties[1].TextBox.Text = model.Duration.ToString();

			listProperties[2].Label.Text = "Text";
			listProperties[2].TextBox.Text = model.Text;

			UnvissibleItems(2);
			VissibleItems(2);
			btPath.Visible = false;

			listProperties[3].Label.Text = "Horizontal position";
			listProperties[4].Label.Text = "Vertical position";

			listProperties[5].Label.Text = "Position X";
			listProperties[5].TextBox.Text = model.X.ToString();

			listProperties[6].Label.Text = "Position Y";
			listProperties[6].TextBox.Text = model.Y.ToString();

			listProperties[7].Label.Text = "Font size";
			listProperties[7].TextBox.Text = model.FontSize.ToString();


			cbAlignmentV.Text = model.AlignmentVertical;
			cbAlignmentH.Text = model.AlignmentHorizontal;


			cbAlignmentH.Visible = true;
			cbAlignmentV.Visible = true;

			listProperties[3].Label.Visible = true;
			listProperties[4].Label.Visible = true;

			listProperties[5].Label.Visible = true;
			listProperties[5].TextBox.Visible = true;

			listProperties[6].Label.Visible = true;
			listProperties[6].TextBox.Visible = true;

			listProperties[7].Label.Visible = true;
			listProperties[7].TextBox.Visible = true;
		}

		/// <summary>
		/// Appears the image model.
		/// </summary>
		/// <param name="templatesShape">The templates shape.</param>
		private void AppearImageModel(TemplatesShape templatesShape)
		{
			ImageStimulusModel model = (ImageStimulusModel)templatesShape.Shape.ModelObject;            

			listProperties[0].Label.Text = "Probability [%]";
			listProperties[0].TextBox.Text = templatesShape.Probability.ToString();

			listProperties[1].Label.Text = "Duration [ms]";
			listProperties[1].TextBox.Text = model.Duration.ToString();

			listProperties[2].Label.Text = "ImagePath";
			listProperties[2].TextBox.Text = model.ImagePath;

			listProperties[3].Label.Text = "Position X";
			listProperties[3].TextBox.Text = model.X.ToString();

			listProperties[4].Label.Text = "Position Y";
			listProperties[4].TextBox.Text = model.Y.ToString();

			UnvissibleItems(4);
			VissibleItems(4);       

			btPath.Visible = true;

			cbAlignmentH.Visible = false;
			cbAlignmentV.Visible = false;
		}

		/// <summary>
		/// Appears the sound model.
		/// </summary>
		/// <param name="templatesShape">The templates shape.</param>
		private void AppearSoundModel(TemplatesShape templatesShape)
		{
			SoundStimulusModel model = (SoundStimulusModel)templatesShape.Shape.ModelObject;

			listProperties[0].Label.Text = "Probability [%]";
			listProperties[0].TextBox.Text = templatesShape.Probability.ToString();

			listProperties[1].Label.Text = "Duration [ms]";
			listProperties[1].TextBox.Text = model.Duration.ToString();

			UnvissibleItems(1);
			VissibleItems(1);
			btPath.Visible = false;
			cbAlignmentH.Visible = false;
			cbAlignmentV.Visible = false;
		}
	}
}
